package com.lbn.monitor.app.monitor.facility;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.free.framework.mvp.BaseMvpPresenter;
import com.lbn.monitor.R;
import com.lbn.monitor.api.model.Pollutant;
import com.lbn.monitor.api.model.RealTimeValue;
import com.lbn.monitor.api.model.XiShu;
import com.lbn.monitor.app.monitor.DataCallBack;
import com.lbn.monitor.base.AppBaseFragment;
import com.lbn.monitor.util.DataUtils;
import com.lbn.monitor.util.UIHandler;
import com.lbn.monitor.widget.calendar.TimeUtils;
import com.lbn.monitor.widget.chart.Element;
import com.lbn.monitor.widget.chart.MonitorView;

import java.text.DecimalFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NlcFragment extends AppBaseFragment implements DataCallBack{


    @BindView(R.id.monitor_chart)
    MonitorView monitorChart;
    private double maxValue = 1000;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_nlc, null, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        if(rootView==null){
            ((FacilityDetailActivity)getActivity()).addCallBack(this);
        }
        super.onViewCreated(view, savedInstanceState);

    }


    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }

    public static NlcFragment newInstance() {

        Bundle args = new Bundle();

        NlcFragment fragment = new NlcFragment();
        fragment.setArguments(args);
        return fragment;
    }

//    @Override
//    public void callBack(RealTimeValue data) {
//        if (monitorChart == null) {
//            return;
//        }
//
//        Pollutant pollutant = ((FacilityDetailActivity) getActivity()).getPollutant();
//        int id = Integer.parseInt(pollutant.getId());
//        String rtd = DataUtils.getRtd(id,data);
//        Double value = Double.valueOf(TextUtils.isEmpty(rtd.trim())?"0":rtd);
//
//        if (value > maxValue) {
//            maxValue = Double.valueOf(new DecimalFormat("#.00").format(value * 1.2));
//            monitorChart.setMaxValue(maxValue);
//        }
//        monitorChart.setTitle(pollutant.getPollutantsFactor());
//        monitorChart.addElement(new Element(TimeUtils.string2Milliseconds(data.getDataTime()), value));
////        UIHandler.postDelayed(new Runnable() {
////            @Override
////            public void run() {
////                FacilityDetailActivity activity = ((FacilityDetailActivity) getActivity());
////                if (activity == null) {
////                    return;
////                }
////                activity.getData();
////            }
////        }, 1000);
//    }

    @Override
    public void callBack(RealTimeValue data) {

    }

    @Override
    public void clear() {
        monitorChart.clear();
    }
}